package com.google.googlemarket.fragement;

import android.support.annotation.StringDef;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.googlemarket.R;
import com.google.googlemarket.adapter.HomeAdapter;
import com.google.googlemarket.adapter.StellarMapAdapter;
import com.google.googlemarket.base.BaseFragment;
import com.google.googlemarket.bean.HomeBean;
import com.google.googlemarket.http.Urls;
import com.google.googlemarket.utils.JsonUtil;
import com.google.googlemarket.utils.Logger;
import com.google.googlemarket.view.LoadMoreListView;
import com.google.googlemarket.view.OnloadingMoreListener;
import com.google.googlemarket.view.stellarmap.StellarMap;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by MrGao on 2017/9/17.
 */

public class RecommendFragment extends BaseFragment {
    public static final int TYPE_INIT_DATA=0;
    public static final int TYPE_INIT_LOADMORE=1;
    private static final int SWITCH_PAGE = 0;

    private StellarMap stellarMap;

    @Override
    public CharSequence getTitle() {
        return "推荐";
    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {
    }

    @Override
    public void initData() {
        requestAsyncTask(TYPE_INIT_DATA);
    }

    @Override
    public int getContentViewLayoutId() {
        return 0;
    }

    @Override
    public View getContentView() {
        stellarMap = new StellarMap(mContext);
        stellarMap.setInnerPadding(10,10,10,10);
        stellarMap.setRegularity(5,15);
        return stellarMap;
    }

    @Override
    protected Object doInBackground(int type) {
        String json = NetUtils.getJson(Urls.RECOMMEND,null);
        return json;
    }

    @Override
    protected void onPostExecute(int requestType,Object result) {
        //这里应该加入判断，防止服务器响应返回数据异常为空导致程序崩溃，预设添加一个数据异常提示
        Logger.i(this,"requestType:"+requestType+",result:"+result.toString());
        Type type = new TypeToken<ArrayList<String>>(){}.getType();

        ArrayList<String> datas = JsonUtil.json2Bean((String) result, type);
        if (checkInitData(datas)){
            stellarMap.setAdapter(new StellarMapAdapter(datas));
            stellarMap.setGroup(0,true);
        }
    }
}
